// pages/personal/personal.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    avatorPicUrl: '',
    idPicUrl1: '',
    idPicUrl2: '',
    picUrl: '',
    idCardName: '',
    idCardNum: null,
    phone: null,
    code: "",
    beforePhone: null,
    info: '未认证',
    isCheck: '',
    send: true,
    check: 0,
    refuseReason: "",
    codename: "获取验证码",
    sureCode: ""
  },

  /**
   * 上传头像图片事件函数
   */
  uploadAvatorPic: function() {
    let that = this;
    wx.chooseImage({
      count: 1,
      success: function(res) {
        that.setData({
          avatorPicUrl: res.tempFilePaths
        })
        var tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url: 'https://www.xiaozheng8.com/upload',
          filePath: tempFilePaths[0],
          name: 'file',
          header: {
            'Content-Type': 'multipart/form-data'
          },
          success: function(res) {
            console.log(res)
            // var picPath = JSON.parse(res.data);
            // console.log(picPath);
            // that.setData({
            //   avatorPicUrl: res.data
            // })
            if (res.statusCode == 200) {
              that.setData({
                avatorPicUrl: res.data
              })
            } else {
              // wx.showToast({
              //   title: '图片上传失败，请重新上传',
              //   icon: "none",
              //   duration: 2000
              // });
              // that.setData({
              //   avatorPicUrl: ""
              // })
            }
            // that.setData({
            //   avatorPicUrl: res.data,
            // })
          }
        })
      }
    })

  },


  /**
   * 上传身份证图片事件函数
   */
  uploadIdPic: function(res) {
    let that = this;
    if (res.currentTarget.dataset.url == 1) {
      wx.chooseImage({
        count: 2,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success: (res) => {
          // 存储临时地址
          this.setData({
            idPicUrl1: res.tempFilePaths,
          })
          var tempFilePaths = res.tempFilePaths
          wx.uploadFile({
            url: 'https://www.xiaozheng8.com/upload',
            filePath: tempFilePaths[0],
            name: 'file',
            header: {
              'Content-Type': 'multipart/form-data'
            },
            success: function(res) {
              console.log(res);
              if (res.statusCode == 200) {
                that.setData({
                  idPicUrl1: res.data
                })
              } else {
                // wx.showToast({
                //   title: '图片上传失败，请重新上传',
                //   icon: "none",
                //   duration: 2000
                // })
              }
            }
          })
        }
      })
    } else if (res.currentTarget.dataset.url == 2) {
      wx.chooseImage({
        count: 2,

        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success: (res) => {
          // 存储临时地址
          this.setData({
            idPicUrl2: res.tempFilePaths,
          })
          var tempFilePaths = res.tempFilePaths
          wx.uploadFile({
            url: 'https://www.xiaozheng8.com/upload',
            filePath: res.tempFilePaths[0],
            name: 'file',
            header: {
              'Content-Type': 'multipart/form-data'
            },
            success: function(res) {
              // var picPath = JSON.parse(res.data);
              // console.log(picPath);
              if (res.statusCode == 200) {
                that.setData({
                  idPicUrl2: res.data
                })
              } else {
                wx.showToast({
                  title: '图片上传失败，请重新上传',
                  icon: "none",
                  duration: 2000
                });
                that.setData({
                  idPicUrl2: ""
                })
              }
              // console.log(res)
              // that.setData({
              //   idPicUrl2: res.data
              // })
            },
            fail: function(res) {
              console.log(res);
              wx.showToast({
                title: '图片上传失败，请重新上传',
                icon: "none",
                duration: 2000
              });
              that.setData({
                idPicUrl2: ""
              })

            }
          })
        }
      })
    } else {
      wx.chooseImage({
        count: 2,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success: (res) => {
          // 存储临时地址
          that.setData({
            idPicUrl1: res.tempFilePaths[0],
            idPicUrl2: res.tempFilePaths[1],
          })
          var tempFilePaths = res.tempFilePaths
          wx.uploadFile({
            url: 'https://www.xiaozheng8.com/upload',
            filePath: that.data.idPicUrl1,
            name: 'file',
            header: {
              'Content-Type': 'multipart/form-data'
            },
            success: function(res) {
              console.log(res)

              that.setData({
                idPicUrl1: res.data
              })
              wx.uploadFile({
                url: 'https://www.xiaozheng8.com/upload',
                filePath: that.data.idPicUrl2,
                name: 'file',
                header: {
                  'Content-Type': 'multipart/form-data'
                },
                success: function(res) {
                  console.log(res)
                  // var picPath = JSON.parse(res.data);
                  // console.log(picPath);
                  // console.log(picPath.data);
                  that.setData({
                    idPicUrl2: res.data
                  })

                  // that.setData({
                  //   idPicUrl2: res.data
                  // })
                },
                fail: function(res) {
                  // console.log(res);
                  // wx.showToast({
                  //   title: '图片上传失败，请重新上传',
                  //   icon: "none",
                  //   duration: 2000
                  // });
                  // that.setData({
                  //   idPicUrl2: ""
                  // })

                }
              })
            },
            fail: function(res) {
              console.log(res);
              wx.showToast({
                title: '图片上传失败，请重新上传',
                icon: "none",
                duration: 2000
              });
              that.setData({
                idPicUrl2: ""
              })
            }
          })
        }
      })
    }

  },


  // 发送验证码

  sendCode: function() {
    var that = this;
    if ((!(/^((13[0-9])|(14[0-9])|(15[0-9])|(17[0-9])|(18[0-9]))\d{8}$/.test(that.data.phone)))) {
      wx.showToast({

        title: '手机号码有误',

        duration: 2000,

        icon: "none"

      })
    } else {
      wx.request({
        url: 'https://www.xiaozheng8.com/sendCode',
        data: {
          phone: that.data.phone,

        },
        method: 'post',
        success: function(res) {
          console.log(res);
        },
        fail: function(res) {},
        complete: function(res) {},
      })
      this.setData({
        companySend: false
      })
      this.timer()
    }


  },
  // 验证码倒计时
  timer: function() {
    var that = this;
    var second = 60;
    this.setData({
      send: false,
      companySend: false
    })
    this.setTimer = setInterval(() => {
      second--;
      that.setData({
        second: second
      })
      that.setData({
        codename: that.data.second + "秒后可重新发送",
        companyCode: that.data.second + "秒后可重新发送"
      })
      console.log(that.data.second);
      if (that.data.second <= 0) {
        that.setData({
          second: 60,
          codename: "重新发送验证码",
          send: true,
          companySend: true
        })

        clearInterval(that.setTimer);

      }
    }, 1000)
  },
  /**
   * 删除图片事件函数
   */
  cancel: function(res) {
    // console.log(res)
    if (res.currentTarget.dataset.num === 1) {
      this.setData({
        avatorPicUrl: '',
        personalImg: ""
      })
    } else if (res.currentTarget.dataset.num === 2) {
      this.setData({
        idPicUrl1: '',
        idCardImgB: ''
      })
    } else {
      this.setData({
        idPicUrl2: '',
        idCardImgP: ''
      })
    }
  },

  /**
   * 获取名字内容
   */
  name: function(e) {

    this.setData({
      idCardName: e.detail.value
    })
    console.log(this.data.idCardName);
  },

  /**
   * 获取电话内容
   */
  phone: function(e) {
    var that = this;
    that.setData({
      phone: e.detail.value
    })
  },

  /**
   * 获取验证码
   */
  code: function(e) {
    this.setData({
      code: e.detail.value
    })
  },

  /**
   * 获取身份证内容
   */
  num: function(e) {
    this.setData({
      idCardNum: e.detail.value
    })
  },
  /**
   * 提交事件
   */

  submit: function() {
    let that = this;
    if (!(/^1[34578]\d{9}$/.test(that.data.phone))) {

    } else if (this.data.beforePhone == this.data.phone && this.data.beforePhone != null) {} else if (this.data.code == "") {
      wx.showToast({

        title: '验证码错误',

        duration: 2000,

        icon: 'none'

      });
      return false;




    } else if (that.data.code != "") {
      wx.request({
        url: 'https://www.xiaozheng8.com/findCode',
        data: {
          phone: that.data.phone
        },
        method: 'post',
        success: function(res) {
          console.log(res);
          if (res.data.data.code === that.data.code) {
            console.log("111")

          } else {
            // console.log("222")
            wx.showToast({

              title: '验证码错误',

              duration: 2000,

              icon: 'none'

            });
            return false
          }

        },
        fail: function(res) {},
        complete: function(res) {},
      })
    }
    if (that.data.idCardName == "") {
      wx.showToast({

        title: '姓名有误',

        duration: 2000,

        icon: 'none'

      });

      return false;

    } else if (!(/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(that.data.idCardNum))) {

      wx.showToast({

        title: '身份证号码有误',

        duration: 2000,

        icon: 'none'

      });

      return false;


    } else if (!(/^1[34578]\d{9}$/.test(that.data.phone))) {

      wx.showToast({

        title: '手机号码有误',

        duration: 2000,

        icon: 'none'

      });

      return false;
    } else if (that.data.avatorPicUrl==""){

      wx.showToast({

        title: '请上传个人头像',

        duration: 2000,

        icon: 'none'

      });
    } else if (that.data.idPicUrl1 == "" || that.data.idPicUrl2==""){
      wx.showToast({

        title: '请上传身份证照',

        duration: 2000,

        icon: 'none'

      });
    } else {
      wx.showModal({
        title: '提示',
        content: "是否确认提交身份认证",
        confirmText: "确认",
        cancelText: "取消",
        success: function(res) {
          if (res.confirm) {
            wx.request({
              url: 'https://www.xiaozheng8.com/auth/person',
              method: 'post',
              data: {
                userId: wx.getStorageSync("userId"),
                idCardName: that.data.idCardName,
                idCardNum: that.data.idCardNum,
                personalImg: that.data.avatorPicUrl,
                idCardImgB: that.data.idPicUrl1,
                idCardImgP: that.data.idPicUrl2,
                phone: that.data.phone
              },
              success: function(res) {
                console.log(res)
                if (res.statusCode == 200) {
                  that.setData({
                    // info: '已认证',
                    isCheck: that.data.idCardNum,
                  })
                  wx.navigateBack({
                    delta: 1,
                  })
                  wx.showModal({
                    title: '提示',
                    content: '信息提交成功',
                    showCancel: false,
                    success: function(res) {
                      if (res.confirm) {

                      }
                    }
                  })
                }
              }
            })

          }
        }
      })
    }

  },
  // 校验验证码
  identifyingCode(phone) {
    var that = this;
    wx.request({
      url: 'https://www.xiaozheng8.com/findCode',
      data: {
        phone: phone
      },
      method: 'post',
      success: function(res) {
        console.log(res);

      },
      fail: function(res) {},
      complete: function(res) {},
    })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;
    wx.request({
        url: 'https://www.xiaozheng8.com/auth/personFind',
        method: 'post',
        data: {
          userId: wx.getStorageSync("userId")
        },
        success: function(res) {
          console.log(res)
          if (res.data.data == null || res.data.data == undefined) {

          } else {
            that.setData({
              idCardName: res.data.data.idCardName,
              idCardNum: res.data.data.idCardNum,
              phone: res.data.data.phone,
              avatorPicUrl: res.data.data.personalImg,
              idPicUrl1: res.data.data.idCardImgB,
              idPicUrl2: res.data.data.idCardImgP,
              info: '已认证',
              personalImg: res.data.data.personalImg,
              idCardImgB: res.data.data.idCardImgB,
              idCardImgP: res.data.data.idCardImgP,
              check: res.data.data.isCheck,
              beforePhone: res.data.data.phone,
              refuseReason: res.data.data.refuseReason
            })
            console.log(res.data.data.personalImg)
          }
        }
      }),
      console.log(this.data.phone);

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    // clearInterval(this.setTimer);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    clearInterval(this.setTimer);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})